#include "Avalon/SignalHandling/SignalSink.hpp"

using namespace Avalon;
using namespace Avalon::SignalHandling;
using namespace boost;
using namespace boost::posix_time;
using namespace std;

SignalSink::SignalEntry::SignalEntry(const type_info& type,
    const vector<any>& parameters)
    : m_type(&type),
      m_parameters(parameters) {}

SignalSink::SignalSink() {}

SignalSink::~SignalSink() {}

SignalSink::SignalEntry SignalSink::GetNextSignal(time_duration timeout) {
  unique_lock<mutex> lock(m_mutex);
  if(m_signalEntries.empty()) {
    if(timeout == pos_infin) {
      m_signalEmptyCondition.wait(lock);
    } else if(!m_signalEmptyCondition.timed_wait(lock, timeout)) {
      BOOST_THROW_EXCEPTION(runtime_error("Signal timed out."));
    }
  }
  SignalEntry entry = m_signalEntries.front();
  m_signalEntries.pop_front();
  return entry;
}
